import { defineStore } from 'pinia';
import permissionsNames from '/@/mock/permissionsNames.json';
import { Local, Session } from '/@/utils/storage';
import { generateTree } from '/@/utils/utils';

/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */
export const useUserInfo = defineStore('userInfo', {
	state: () => ({
		userInfos: {
			id: '',
			supplier_id: '',
			type: 1,
			menu: '',
			userName: '',
			username: '',
			title: '',
			photo: '',
			time: 0,
			menuJson: [],
			roles: [],
			authBtnList: [],
		},
	}),
	actions: {
		async setUserInfos(data: Object) {
			// 存储用户信息到浏览器缓存
			if (Session.get('userInfo')) {
				this.userInfos = Session.get('userInfo');
			} else {
				const userInfos: any = await this.getApiUserInfo(data || Local.get('userInfo'));
				this.userInfos = userInfos;
			}
		},
		async getApiUserInfo(data: any) {
			return new Promise((resolve) => {
				setTimeout(() => {
					const permissionsData = data.permissions.filter((item: any) => item.menu_type === 1);
					const permissions = permissionsData.map((item: any) => {
						const component_alias = JSON.parse(item.component_alias);
						console.log(
							'adqadff',
							data.permissions_names.findIndex((item1: any) => item1 === `${component_alias.roles[0]}Menu`) === -1 ? true : false
						);

						if (item.redirect) {
							return {
								id: item.id,
								pid: item.pid,
								path: item.path,
								name: item.name,
								component: item.component,
								redirect: item.redirect,
								meta: {
									...component_alias,
									title: component_alias.title.replace('(供应商)', ''),
									isKeepAlive: component_alias.is_keep_alive ? true : false,
									isHide:
										data.permissions_names.findIndex((item1: any) => item1 === `${component_alias.roles[0]}Menu`) === -1
											? true
											: component_alias.is_hide
											? true
											: false,
								},
							};
						} else {
							return {
								id: item.id,
								pid: item.pid,
								path: item.path,
								name: item.name,
								component: item.component,
								meta: {
									...component_alias,
									title: component_alias.title.replace('(供应商)', ''),
									isKeepAlive: component_alias.is_keep_alive ? true : false,
									isHide:
										data.permissions_names.findIndex((item1: any) => item1 === `${component_alias.roles[0]}Menu`) === -1
											? true
											: component_alias.is_hide
											? true
											: false,
								},
							};
						}
					});
					let menuJson1 = generateTree(permissions, 0, { idName: 'id', pidName: 'pid', childrenName: 'children' });
					console.log('userInfos.value.menuJson', menuJson1);
					let defaultRoles: Array<string> = [];
					let defaultAuthBtnList: Array<string> = data.username === 'admin' ? permissionsNames : data.permissions_names;
					// 用户信息模拟数据
					const userInfos = {
						userName: data.username,
						photo: data.avatar || 'https://img2.baidu.com/it/u=1978192862,2048448374&fm=253&fmt=auto&app=138&f=JPEG?w=504&h=500',
						time: new Date().getTime(),
						roles: defaultRoles,
						authBtnList: defaultAuthBtnList,
						menuJson: menuJson1,
						...data,
					};
					Session.set('userInfo', userInfos);
					resolve(userInfos);
				}, 0);
			});
		},
	},
});
